/** @file    persistence/Qt/EncoderWriterXml.cpp
 *  @author  Alessandro Polo
 *  @version $Id: EncoderWriterXml.cpp 1917 2010-02-22 12:05:14Z alex $
 *  @brief
 * File containing methods for the wosh::persistence::EncoderWriterXml class.
 * The header for this class can be found in EncoderWriterXml.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/persistence/Qt/EncoderWriterXml.h>

 #include <QtXml>


using namespace std;
namespace wosh {
 namespace persistence {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

EncoderWriterXml::EncoderWriterXml() : IEncoderWriter()
 {
	this->doc = NULL;
	this->currentNode = NULL;

	this->explicitTypes = true;

	Log.setContext( "EncoderWriterXml" );
	Log.setIndent( 3 );
	Log.setLevel( LOG_INFO );
Log.setLevel( LOG_VERBOSE );

 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderWriterXml::open( const std::string& filename )
 {
	if ( this->doc != NULL )
		return WRET_ERR_ILLEGAL_USE;

	this->doc = new QDomDocument("none");
	this->currentNode = NULL;
	this->docFilename = filename;

	this->currentNode = new QDomElement();
	*this->currentNode = this->doc->createElement("WOSH");
	this->doc->appendChild(*this->currentNode);

	return WRET_OK;
 }

WRESULT EncoderWriterXml::close()
 {
	if ( this->doc == NULL )
		return WRET_ERR_ILLEGAL_USE;

	QFile File( this->docFilename.c_str() );
	if ( File.open( QIODevice::WriteOnly ) ) {
		QTextStream TextStream(&File);
		TextStream << doc->toString(4);
		File.close();
	 }
	else 
		Log(LOG_CRITICAL, ":saveXmlDocument() can't load file %s", this->docFilename.c_str() );

	this->currentNode = NULL;

	delete this->doc; this->doc = NULL;

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderWriterXml::push( const ISerializable* object, const std::string& field_name, SerializeStrategy strategy )
 {
	if ( object == NULL ) return WRET_ERR_PARAM;
	if ( this->currentNode == NULL )
		return WRET_ERR_INTERNAL;

	switch (strategy) {
		case SERIALIZE_NODE: {

				string field_nameTmp = field_name;
				if ( field_nameTmp.empty() )
					field_nameTmp = string( object->getClassName() ); // make it simplified?

				QDomElement newNode = this->doc->createElement( field_nameTmp.c_str() );
				newNode.setAttribute( "type", object->getClassName() );

				QDomElement previousCurrentNode = *this->currentNode;
				// recursive
				*this->currentNode = newNode;
				WRESULT ret = object->store(this);
				*this->currentNode = previousCurrentNode;
				this->currentNode->appendChild(newNode);

				break;
			 }
		case SERIALIZE_FIELD:
		case SERIALIZE_DEFAULT:
		default: {
				this->currentNode->setAttribute( field_name.c_str(), object->toString().c_str() );
				break;
			 }
	 }

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderWriterXml::push( const std::string& obj, const std::string& field_name, SerializeStrategy strategy )
 {
	if ( this->currentNode == NULL )
		return WRET_ERR_INTERNAL;

	switch (strategy) {
		case SERIALIZE_NODE: {
				// create new (string) node
				createNode( field_name, "string", obj.c_str() );

				break;
			 }
		case SERIALIZE_FIELD:
		case SERIALIZE_DEFAULT:
		default: {
				// create new (string) attribute
				this->currentNode->setAttribute( field_name.c_str(), obj.c_str() );
				break;
			 }
	 }

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT EncoderWriterXml::push( const long obj, const std::string& field_name, SerializeStrategy strategy )
 {
	if ( this->currentNode == NULL )
		return WRET_ERR_INTERNAL;

	switch (strategy) {
		case SERIALIZE_NODE: {
				// create new node
				createNode( field_name, "int", obj );
				break;
			 }
		case SERIALIZE_FIELD:
		case SERIALIZE_DEFAULT:
		default: {
				// create new (string) attribute
				this->currentNode->setAttribute( field_name.c_str(), QString::number(obj) );
				break;
			 }
	 }

	return WRET_OK;
 }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace persistence
}; // namespace wosh
